home *** CD-ROM | disk | FTP | other *** search
Text File | 1986-12-01 | 2.0 KB | 77 lines | [TEXT/MPS ] |
- (* @(#) StringFormat.inc.p 11/28/86
-
- WHAT: Implementation of UNIT StringWrite
- WHO: Joel West, Western Software Technology
- WHEN: November 1986
- HOW: Formatted output to Pascal strings. Names match
- Modula-2 InOut module. Developed to replace —
- albeit awkwardly — use of C sprintf.
- WHY: Included from StringFormat.p (INTERFACE) for IMPLEMENTATION
- *)
-
- { As with all the Pascal equivalents, output the specified field width
- or the minimum necessary number of digits, whichever is greater.
- Does not check for the output string > 255 characters
- }
-
- {*-----------------------------------*
- | SWrite -- format a character |
- *-----------------------------------*}
- PROCEDURE SWrite(VAR s: Str255; c: CHAR);
- VAR
- i : INTEGER;
- BEGIN
- i := ORD(s[0])+1;
- s[0] := CHR(i);
- s[i] := c;
- END; (* SWrite *)
-
- {*-------------------------------------------*
- | SWriteHex -- format a number in hex |
- *-------------------------------------------*}
- PROCEDURE SWriteHex(VAR s: Str255; n: LongInt; w: INTEGER);
- VAR
- d: INTEGER;
- s2: Str255;
- BEGIN
- s2[0] := CHR(w);
- WHILE w > 0 DO
- BEGIN
- d := BAND(n,$F);
- n := BSR(n,4);
- IF d < 10 THEN
- s2[w] := CHR(ORD('0') + d)
- ELSE
- s2[w] := CHR(ORD('A')-10+d);
- w := w-1;
- END;
- SWriteString(s,s2);
- END; (* SWriteHex *)
-
- {*-------------------------------------------*
- | SWriteInt -- format a number in decimal |
- *-------------------------------------------*}
- PROCEDURE SWriteInt(VAR s: Str255; n: LongInt; w: INTEGER);
- VAR
- i: INTEGER;
- s2: Str255;
- BEGIN
- NumToString(n, s2);
- i := w - Length(s2);
- WHILE i > 0 DO
- BEGIN
- SWrite(s, ' '); (* Leading spaces *)
- i := i-1;
- END;
- SWriteString(s,s2);
- END; (* SWriteInt *)
-
- {*-------------------------------------------*
- | SWriteString -- format a character string |
- *-------------------------------------------*}
- PROCEDURE SWriteString(VAR s: Str255; s2: Str255);
- BEGIN
- s := Concat(s,s2);
- END; (* SWriteString *)
-
-